package com.base.controller;

import com.base.po.tbk.TbkGoodContent;
import com.base.po.voip.Users;
import com.base.service.TbkGoodContentService;
import com.base.service.UsersService;
import com.base.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 好货专场
 *
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class index_GoodContent {
	@Autowired
	private TbkGoodContentService tbkGoodContentService;
	@Autowired
	private UsersService usersService;

	// 精选单品首页
	@RequestMapping("/goodContentIndex")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		Page page = new Page();
		Users users = usersService.selectByPrimaryKey(adminUserId);
		List<TbkGoodContent> tbkGoodContent = null;

		page.setCurrentPageNo(1);
		page.setRecordTotal(tbkGoodContentService.countAll(adminUserId));
		tbkGoodContent = tbkGoodContentService.selectAll(page, adminUserId);

		request.setAttribute("totlenum", page.getRecordTotal());// 传入session中
		request.setAttribute("curPage", page.getCurrentPageNo());// 传入session中
		request.setAttribute("tbkGoodContent", tbkGoodContent);
		// 设置页面进行跳转
		modelAndView.setViewName("jsp/index_GoodContent");
		return modelAndView;
	}

	@RequestMapping("/selectGoodContent")
	public ModelAndView selectGoodContent(HttpServletRequest request, Integer curPage) throws Exception {
		// 设置session缓存
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}

		String adminUserId = (String) request.getSession().getAttribute("adminUserId");

		ModelAndView modelAndView = new ModelAndView();
		Page page = new Page();
        Users users = usersService.selectByPrimaryKey(adminUserId);

		List<TbkGoodContent> tbkGoodContent = null;

        page.setCurrentPageNo(curPage);
        page.setRecordTotal(tbkGoodContentService.countAll(adminUserId));
		tbkGoodContent = tbkGoodContentService.selectAll(page, adminUserId);

        request.setAttribute("totlenum", page.getRecordTotal());// 传入session中
        request.setAttribute("curPage", page.getCurrentPageNo());// 传入session中
        request.setAttribute("tbkGoodContent", tbkGoodContent);
        // 设置页面进行跳转
        modelAndView.setViewName("jsp/index_GoodContent");
		return modelAndView;
	}
}
